/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui.activities.adapters;

import java.util.List;

import utsc.apps.utschedule.R;
import utsc.apps.utschedule.classes.Course;
import utsc.apps.utschedule.classes.UofTGrade;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * A simple list adapter to show a list of {@link Course courses} along with their grades. 
 */
public class CourseListAdapter extends ArrayAdapter<Course> {
	
	public CourseListAdapter(Context context, int textViewResourceId,
			List<Course> objects) {
		super(context, textViewResourceId, objects);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		TextView _txtPrefix;
		//first inflate our view and cache the ui elements into its viewholder object
		Course c=getItem(position);
		
		if (convertView==null) {
			holder=new ViewHolder();
			convertView=LayoutInflater.from(getContext()).inflate(R.layout.course_list_item, null);
			holder._txtCourseCode=(TextView) convertView.findViewById(R.id.txt_list_course);
			holder._colorCourse=(TextView) convertView.findViewById(R.id.color_cell_course);
			_txtPrefix=(TextView) convertView.findViewById(R.id.txt_list_innerbox_prefix);
			_txtPrefix.setText(null);
			holder._txtCourseGrade=(TextView) convertView.findViewById(R.id.txt_list_innerbox_content);
			convertView.setTag(holder);
		}else {
			//always retrieve the viewholder from the tag
			holder=(ViewHolder) convertView.getTag();
		}
		holder._txtCourseCode.setText(c.get_courseCode());
		Drawable colorDrawable=holder._colorCourse.getBackground();
		UofTGrade grade=c.get_grade();
		if (grade==null || grade.get_marks()==0) {
			holder._txtCourseGrade.setText("0%");
		}else {
			holder._txtCourseGrade.setText(grade.toPercentageString());
		}
		
		//sets the course color 
		if (colorDrawable instanceof LayerDrawable) {
			((LayerDrawable) colorDrawable).getDrawable(0).setColorFilter(c.get_color(), PorterDuff.Mode.SRC_ATOP);
		}
		return convertView;
	}
	
	private static class ViewHolder{
		TextView _txtCourseCode;
		TextView _colorCourse;
		TextView _txtCourseGrade;
	}

}
